/**
 * @(#) MedicalMoneyProxy.as
 *
 * @date		2011. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	건강보험증 Proxy - Proxy
 * 				건강보험증를 조회를한다.
 */
package com.skt.common_hr.retirement.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.retirement.model.vo.MemberVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class RetirementProxy extends CommonProxy
	{
		public static const NAME:String = "RetirementProxy";

		public static const LOAD_MEDICALMONEY_LIST_COMPLETE:String	= "LoadMedicalMoneyListComplete";
		public static const LOAD_MEDICALMONEY_LIST_FAILED:String	= "LoadMedicalMoneyListFailed";


		public var mainList:ArrayCollection;
		public var subTabList:ArrayCollection;
		public var acOitab:ArrayCollection;

		public var key:String;

		//지원기준
		public var loadFaultMessage:String;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function RetirementProxy(data:Object=null)
		{
			super(NAME, data);
		}

		// 메인리스트 호출
		public function loadList(setFunc:Function):void
		{
			var obj:Object = {};
			obj.I_PERNR	= Variables.myPernr;
			loadData("Z_HR_0157", obj, loadListResult, listFault, setFunc);
		}
		public function loadListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				mainList = event.result.T_RESULT as ArrayCollection;
				event.result.func(mainList, event.result);
			}
			else {
				Alert.show(retText);
			}
		}

		// 부서 treedata
		public function loadTree(pernr:String, auth:String, setFunc:Function):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_AUTH"] = auth;

			loadData("Z_HR_0015", param, loadTreeResult, listFault, setFunc);
		}
		public function loadTreeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var orgList:XMLList = parseOrgList(event.result.RTAB);
				event.result.func(orgList)
			}
			else {
				Alert.show('loadTreeResult err: ' + retText);
			}
		}

		/**
		 * @return void
		 */
		public function loadMemberSearch(orgeh:String, keyword:String, setFunc:Function):void
		{
			var param:Object = new Object();
			param["I_ORGEH"] = orgeh;
			param["I_KEYWORD"] = keyword;

			loadData("Z_HR_0016", param, loadMemberSearchResult, listFault, setFunc);
		}
		public function loadMemberSearchResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var memberList:ArrayCollection = parseMemberList(event.result.RTAB);
				var obj:Object = {};
				obj.ENAME = "전체"
				obj.PERNR = "";
				memberList.addItemAt(obj, 0);
				event.result.func(memberList)
			}
			else {
				Alert.show('loadMemberSearchResult err: ' + retText);
			}
		}

		/**
		 * 팝업창에서 사용하는.. 멤버 구하기.
		 */
		public function loadMemberSearch2(orgeh:String, keyword:String, setFunc:Function):void
		{
			var param:Object = new Object();
			param["I_ORGEH"] = orgeh;
			param["I_KEYWORD"] = keyword;

			loadData("Z_HR_0016", param, loadMemberSearchResult2, listFault, setFunc);
		}
		public function loadMemberSearchResult2(event:ResultEvent):void
		{
			if(retCode==0) {
				var memberList:ArrayCollection = parseMemberList(event.result.RTAB);
				event.result.func(memberList)
			}
			else {
				Alert.show('loadMemberSearchResult2 err: ' + retText);
			}
		}

		private function parseOrgList(retObj:ArrayCollection):XMLList
		{
			var prevArr:Array = new Array();
			var root:XML;
			if(retObj.length>0) {
				root = <node label={retObj[0].STEXT} objid={retObj[0].OBJID} level={retObj[0].LEVEL} parent={retObj[0].UPPERORG}/>;
				prevArr[root.@level] = root;

				for(var i:int=1; i<retObj.length; i++)
				{
					var rec:Object = retObj[i];
					var node:XML = <node label={rec.STEXT} objid={rec.OBJID} level={rec.LEVEL} parent={rec.UPPERORG}/>;
					if(prevArr[parseInt(rec.LEVEL)-1]!=null) {
						XML(prevArr[parseInt(rec.LEVEL)-1]).appendChild(node);
					}
					prevArr[node.@level] = node;
				}
			}
			else {
				root = <node label="No Data" objid="0000" level="0" parent="0000"/>;
			}
			return new XMLList(root);
		}






















		// 가족관계
		public var familyList:ArrayCollection;
		public function loadCommonCode(code:String, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = code;
			loadData("Z_HR_0003", params, loadCommonCodeResult, listFault, setFunc);
		}
		public function loadCommonCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var ac:ArrayCollection = event.result.ITAB;
				var obj:Object = {};
				obj.TEXT = "전체"
				obj.CODE = "";
				ac.addItemAt(obj, 0);
				event.result.func(ac);
			}
			else {
				Alert.show('loadCommonCodeResult err: ' + retText);
			}
		}


		// 리스트 업데이트
 /* 		public function detailList(selecteItem:Object, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_BUKRS"] = selecteItem.BUKRS;
			params["I_PERNR"] = selecteItem.PERNR;
			params["I_REQDT"] = selecteItem.REQDT_JSON;
			params["I_REQRN"] = selecteItem.REQRN;

			loadData("Z_HR_0142", params, detailListResult, listFault, setFunc);
		}
		public function detailListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				subTabList 	= parseMainList(event.result.O_ITAB);
				acOitab		= parseSubList(event.result.O_FAM);

				var obj:Object = {};
				obj.acOitab		= subTabList;
				obj.acOfam	 	= acOitab;

				event.result.func(obj)
			}
			else {
				Alert.show('detailListResult err: ' + retText);
			}
		} */


		// 삭제
 		public function del(_GWAREKEY:String, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_GWAREKEY"] = _GWAREKEY;
			//params["I_PERNR"] = Variables.myPernr;
			loadData("Z_HR_H002", params, delResult, listFault, setFunc);
		}
		public function delResult(event:ResultEvent):void
		{
			if(retCode==0) {
				event.result.func();
			}
			else {
				Alert.show('delResult err: ' + retText);
			}
		}

		// 재발급 사유 코드
		public var acReReqRsnCode:ArrayCollection;
		public function getReReqRsnCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDWHYRE";
			loadData("Z_HR_0003", params, getReReqRsnCodeResult, listFault, setFunc);
		}
		public function getReReqRsnCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"재발급사유를 선택하세요"};
				acReReqRsnCode = event.result.ITAB;
				acReReqRsnCode.addItemAt(obj, 0);
				event.result.func(acReReqRsnCode);
			}
			else {
				Alert.show('getReReqRsnCodeResult err: ' + retText);
			}
		}

		// 관계 코드조회
		public var acFamilyCode:ArrayCollection;
		public function getFamilyCode(setFunc:Function):void
		{
			var params:Object = new Object();
			loadData("Z_HR_0144", params, getFamilyCodeResult, listFault, setFunc);
		}
		public function getFamilyCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", ATEXT:"선택하세요", AUSPR:"0"};
				acFamilyCode = event.result.O_ITAB;
				acFamilyCode.addItemAt(obj, 0);
				event.result.func(acFamilyCode);
			}
			else {
				Alert.show('getFamilyCodeResult err: ' + retText);
			}
		}

		// 변경신청항목 코드조회
		public var acEditListCode:ArrayCollection;
		public function getEditListCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDCWHAT";
			loadData("Z_HR_0003", params, getEditListCodeResult, listFault, setFunc);
		}
		public function getEditListCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"선택하세요"};
				acEditListCode = event.result.ITAB;
				acEditListCode.addItemAt(obj, 0);
				event.result.func(acEditListCode);
			}
			else {
				Alert.show('getEditListCodeResult err: ' + retText);
			}
		}

		// 피부양자 신고 구분
		public var acDependantsDvCode:ArrayCollection;
		public function getDependantsDvCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDIETYP";
			loadData("Z_HR_0003", params, getDependantsDvCodeResult, listFault, setFunc);
		}
		public function getDependantsDvCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"선택하세요"};
				acDependantsDvCode = event.result.ITAB;
				acDependantsDvCode.addItemAt(obj, 0);
				event.result.func(acDependantsDvCode);
			}
			else {
				Alert.show('getDependantsDvCodeResult err: ' + retText);
			}
		}

		// 추가/제외사유
		public var acDependantsRsnCode:ArrayCollection;
		public function getDependantsRsnCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDIERSN";
			loadData("Z_HR_0003", params, getDependantsRsnCodeResult, listFault, setFunc);
		}
		public function getDependantsRsnCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"선택하세요"};
				acDependantsRsnCode = event.result.ITAB;
				acDependantsRsnCode.addItemAt(obj, 0);
				event.result.func(acDependantsRsnCode);
			}
			else {
				Alert.show('getDependantsRsnCodeResult err : ' + retText);
			}
		}



		// 저장
		public var _dv:String;
		public function sendSave(obj:Object, setFunc:Function, dv:String, gwarekey:String=''):void
		{
			// 임시저장인지 결재까지인지 체크..변수 1:TEMP, 2:APPROVAL
			_dv = dv;

			var tables:Object = new Object();
			var table:Array = new Array();
			var params:Object = new Object();

			params["I_GWAREKEY"] 	= gwarekey;

			obj = toJsonType(obj);
//			obj.RBEG = DateUtil.toBasicString(obj.RBEG);
//			obj.REND = DateUtil.toBasicString(obj.REND);
//			obj.AWDDT= DateUtil.toBasicString(obj.AWDDT);

			table.push(obj);
			tables["ITAB"] = table;

			saveData("Z_HR_H003", params, tables, saveOrderListResult, listFault, setFunc);
		}


		public function saveOrderListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				//event.result.func(_dv, event.O_GWAREKEY.toString() );
				var gwKey:String = event.result.O_GWAREKEY;
				event.result.func(_dv, gwKey);
			}
			else {
				Alert.show(retText);
			}
		}


		private function parseMainList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var obj:Object in recArr){
				var retObj:Object = new Object();
				for(var field:String in obj) {
					if(field == "PAYDT" || field == "PAYDT" ) {
						//retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field]);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field]);
					}
					else if(field == " AMT01" || field == " AMT02" || field == " AMT03" 
							|| field == " AMT04" || field == " AMT05" || field == " AMT06") {
						retObj[field] = Math.round(obj[field]*100);
					}					
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}

		private function toJsonType(vo:Object):Object {
			var retObj:Object = new Object;
			for(var field:String in vo) {
				if(vo[field] is Date) {
					retObj[field] = DateUtil.toBasicString(vo[field]);
				}
				else {
					retObj[field] = vo[field];
				}
				trace(retObj[field]);
			}
			return retObj;
		}

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}


		private function parseMemberList(retObj:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in retObj)
			{
				var vo:MemberVO = new MemberVO();
				vo.ORGEH 		= rec.ORGEH;
				vo.STEXT 		= rec.STEXT;
				vo.PERNR 		= StringUtil.trimFront(rec.PERNR, "0");
				vo.ENAME 		= rec.ENAME;
				vo.PLANST 		= rec.PLANST;
				vo.PTEXT 		= rec.PTEXT;
				vo.STELLT 		= rec.STELLT;
				vo.JWTXT 		= rec.JWTXT;
				vo.TEL_OFFICE 	= rec.TEL_OFFICE;
				vo.TEL_MOBILE1	= rec.TEL_MOBILE1;
				vo.TEL_MOBILE2 	= rec.TEL_MOBILE2;
				vo.WKADDR		= rec.WKADDR;
				vo.PHOTO_URL 	= rec.PHOTO_URL;
				if(vo.PHOTO_URL.length<1) {
					vo.PHOTO_URL = "assets/images/noimage.swf";
				}
				vo.BAND 		= rec.BAND;
				vo.PERSK 		= rec.PERSK;
				list.addItem(vo);
			}
			return list;
		}
	}
}